#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>

#include "floyd_warshall.h"

void init_array(int n, int path[60][60]) {
    int i, j;

    for (i = 0; i < n; i++)
        for (j = 0; j < n; j++) {
            path[i][j] = i * j % 7 + 1;
            if ((i + j) % 13 == 0 || (i + j) % 7 == 0 || (i + j) % 11 == 0)
                path[i][j] = 999;
        }
}

void print_array(int n, int path[60][60])

{
    int i, j;

    fprintf(stderr, "==BEGIN DUMP_ARRAYS==\n");
    fprintf(stderr, "begin dump: %s", "path");
    for (i = 0; i < n; i++)
        for (j = 0; j < n; j++) {
            if ((i * n + j) % 20 == 0)
                fprintf(stderr, "\n");
            fprintf(stderr, "%d ", path[i][j]);
        }
    fprintf(stderr, "\nend   dump: %s\n", "path");
    fprintf(stderr, "==END   DUMP_ARRAYS==\n");
}

int main() {
    int n = 60;
    int has_errors = 0;

    int path[60][60];
    int check_path;

    init_array(n, path);

    kernel_floyd_warshall(path);

    FILE *tb_file = fopen("check.data", "r");
    assert(tb_file != NULL && "Couldn't open check data file");

    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {
            int res = fscanf(tb_file, "%d", &check_path);
            assert(res == 1 && "read check data file failed");
            if (path[i][j] != check_path) {
                printf("path[%d][%d] = %d, expected_path[%d][%d] = %d\n",
                       i, j,
                       path[i][j],
                       i, j,
                       check_path);
                has_errors++;
            }
        }
    }

    fclose(tb_file);

    if (has_errors) {
        fprintf(stderr, "Benchmark results are incorrect\n");
        return -1;
    }

    printf("Success.\n");
    return 0;
}
